//
//  MKAPITestTimerAPIBase.m
//  MKAPITests
//
//  Created by 苏尚进 on 2022/8/15.
//  Copyright © 2022 moon. All rights reserved.
//

#import "MKAPITestTimerAPIBase.h"

@interface MKAPITestTimerAPIBase()
@property(nonatomic,assign) NSTimeInterval duration;
@property(nonatomic,strong) NSTimer *timer;
@property(nonatomic,assign) CGFloat preProgressPercent;
@property(nonatomic,assign) BOOL progressIncreat;
@property(nonatomic,assign) BOOL progressOver50Percent;
@end

@implementation MKAPITestTimerAPIBase
- (id)initWithDuration:(NSTimeInterval)duration{
    if (self=[super init]) {
        self.duration = duration;
    }
    return self;
}
- (XCTestExpectation *)createCancelExpectationsWithTestCase:(XCTestCase *)testCase{
    self.cancelExpectation = [testCase expectationWithDescription:[NSString stringWithFormat:@"%@.cancelExpectation",NSStringFromClass(self.class)]];
    return self.cancelExpectation;
}
- (XCTestExpectation *)createCompletionExpectationsWithTestCase:(XCTestCase *)testCase{
    self.completionExpectation = [testCase expectationWithDescription:[NSString stringWithFormat:@"%@.completionExpectation",NSStringFromClass(self.class)]];
    return self.completionExpectation;
}
- (XCTestExpectation *)createBeginExpectationsWithTestCase:(XCTestCase *)testCase{
    self.beginExpectation = [testCase expectationWithDescription:[NSString stringWithFormat:@"%@.beginExpectation",NSStringFromClass(self.class)]];
    return self.beginExpectation;
}
- (XCTestExpectation *)createProgressExpectationsWithTestCase:(XCTestCase *)testCase{
    self.progressExpectation = [testCase expectationWithDescription:[NSString stringWithFormat:@"%@.progressExpectation",NSStringFromClass(self.class)]];
    return self.progressExpectation;
}
- (id)init{
    return [self initWithDuration:2];
}
- (void)willBeginRunning{
    [super willBeginRunning];
    [self.beginExpectation fulfill];
    if(self.progressExpectation){
        self.progressIncreat = YES;
        self.preProgressPercent = self.progress.mkapi_progressPercent;
    }
}
- (void)doRunTimerTask{
    NSTimeInterval duration = self.duration;
    NSInteger totalUnitCount = 100;
    @MKAPI_WEAKIFY(self);
    NSTimeInterval interval = 0.1;
    self.timer = [NSTimer mkapitest_scheduledTimerWithTimeInterval:interval repeats:YES block:^(NSTimer * _Nonnull timer) {
        @MKAPI_NORMALIZE(self);
        CGFloat percent = interval/duration;
        CGFloat completedUnitCount = self.progress.completedUnitCount+percent*totalUnitCount;
        
        [self updateProgress:^(NSProgress * _Nonnull progress) {
            if(completedUnitCount<=progress.totalUnitCount){
                progress.completedUnitCount = completedUnitCount;
            }
        }];
        if(completedUnitCount>=self.progress.totalUnitCount){
            [self completeWithError:nil];
        }
    }];
}
- (void)doRunTask{
    [super doRunTask];
    [self doRunTimerTask];
}
- (void)didUpdateProgress{
    [super didUpdateProgress];
    if(self.progressExpectation){
        CGFloat progressPercent = self.progress.mkapi_progressPercent;
        NSLog(@"progress:progressExpectation:%@,progressPercent:%@,preProgressPercent:%@",self.progress.mkapi_description,@(progressPercent),@(self.preProgressPercent));
        if(progressPercent<self.preProgressPercent){
            NSLog(@"progressPercent(%.2f)<preProgressPercent(%.2f),%@",progressPercent,self.preProgressPercent,self.progress.mkapi_description);
            self.progressIncreat = NO;
        }
        if(progressPercent<0||progressPercent>1){
            NSLog(@"progressPercent(%.2f) out of [0,1]",progressPercent);
            self.progressIncreat = NO;
        }
        if((self.preProgressPercent<=0.5&&progressPercent>0.5) || (self.preProgressPercent<0.5&&progressPercent>=0.5)){
            self.progressOver50Percent = YES;
        }
        
        self.preProgressPercent = progressPercent;
    }
}
- (void)didCompleteWithError:(nullable NSError *)error{
    [super didCompleteWithError:error];
    [self.timer invalidate];
    [self.completionExpectation fulfill];
    if(self.progressExpectation){
        if(self.progressIncreat && self.progressOver50Percent){
            [self.progressExpectation fulfill];
        }else if(!self.progressOver50Percent){
            NSLog(@"progres no over 0.5");
        }
    }
}
- (void)doCancel{
    [super doCancel];
    [self.timer invalidate];
    [self.cancelExpectation fulfill];
    [self completeWithError:[NSError errorWithDomain:@"MKAPITest" code:MKAPITaskCodeCancel userInfo:@{NSLocalizedDescriptionKey:@"Cancel"}]];
}
- (void)dealloc{
    [self.timer invalidate];
}
@end

@implementation MKAPITestTimerAPISuccess
- (id)init{
    if (self=[super init]) {
        [self completeWithError:nil];
    }
    return self;
}
@end
